<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 16-12-5
 * Time: 下午9:26
 */
namespace app\wateradmin\controller;

use app\common\controller\BaseAdmin;
use think\Db;
class Admincustom extends BaseAdmin{
    /*
     * 后台首页
     * 赵坤
     * 20161206
     * */
    public function index(){
        $this->assign('title','拼图云客服管理');
        $this->assign('description','拼图云商城');
        if(isset($_GET['key'])&&!empty($_GET['key'])){
            $this->assign('key',trim($_GET['key']));
        }
        $custom = model('ShopSeller')->getSellerList('');
        $this->assign('customlist',$custom['list']);
        $this->assign('customcount',$custom['count']);
        return $this->fetch();
    }
   /*
    * 客服添加
    * 赵坤
    * 20161206
    * */
    public function add(){
        $this->assign('title','拼图云客服添加');
        $this->assign('description','拼图云商城');

        if($this->request->isPost()){
            $group = $_POST['group'];
            $groupname = $_POST['groupname'];
            if(is_array($group)&&!empty($group)){
                $group = implode(',',$group);
                $groupname = implode(',',$groupname);
            }else{
                return json(array('status'=>0,'info'=>'权限组不能为空'));
            }
            $seller_info = array(
                'seller_name'=>$this->request->param('seller_name'),
                'seller_company'=>session('seller_company'),
                'pass'=>md5($this->request->param('seller_pass')),
                'seller_tel'=>$this->request->param('seller_tel'),
                'seller_truename'=>$this->request->param('seller_truename'),
                'seller_avatar'=>$this->request->param('headimg'),
                'seller_group_id'=>$groupname,
                'seller_area'=>session('seller_area'),
                'seller_address'=>session('seller_address'),
                'seller_email'=>session('seller_email'),
                'is_admin'=>0,
                'parent_id'=>session('shop'),
            );
            $res = model('ShopSeller')->addSeller($seller_info);
            if($res){
                $group = explode(',',$group);
                $seller_group = array();
                foreach ($group as $k=>$g){
                    $auth = array(
                        'uid'=>$res,
                        'group_id'=>$g
                    );
                    array_push($seller_group,$auth);
                }
                Db::name('shop_auth_group_access')->insertAll($seller_group);
                return json(array('status'=>1));
            }else{
                return json(array('status'=>0,'info'=>'添加失败'));
            }
        }
        $group = Db::name('shop_auth_group')->where(array('seller_id'=>session('shop')))->select();
        $this->assign('group',$group);
        return $this->fetch();
    }
    /*
     * 拼图云客服组
     * 赵坤
     * */
    public function groupList(){
        $this->assign('title','拼图云客服组添加');
        $this->assign('description','拼图云商城');
        $group = Db::name('shop_auth_group')->where(array('seller_id'=>session('shop')))->select();
        foreach ($group as $key=>$value){
            if(!empty($value['rules'])){
                $rules = explode(',',$value['rules']);
                $desc = '';
                foreach ($rules as $k=>$v){
                    $desc .= Db::name('shop_auth_rule')->where('id',$v)->value('title');
                    if(count($rules)-1>$k){
                        $desc .=',';
                    }
                }
            }
            $group[$key]['desc'] = $desc;
        }
        $this->assign('group',$group);
        return $this->fetch();
    }
    /*
   * 客服组添加
   * 赵坤
   * 20161206
   * */
    public function addGroup(){
        $this->assign('title','拼图云客服组添加');
        $this->assign('description','拼图云商城');
        if($this->request->isPost()){
            $count = Db::name('shop_auth_group')->where(array('seller_id'=>session('shop')))->count();
            if($count+1>16){
                return json(array('status'=>0,'info'=>'已超过16组用户权限'));
            }
            $rules = $_POST['rule'];
            $rules = implode(',',$rules);
            $group_info = array(
                'title'=>$this->request->param('title'),
                'seller_id'=>session('shop'),
                'rules'=>$rules
            );
            $id = Db::name('shop_auth_group')->insertGetId($group_info);
            if($id!==false){
                return json(array('status'=>1));
            }else{
                return json(array('status'=>0,'info'=>'服务器繁忙,稍后重试'));
            }
        }
        $rules = Db::name('shop_auth_rule')->where(array('parent_id'=>0))->select();
        foreach ($rules as $key=>$value){
            $rules[$key]['child']=Db::name('shop_auth_rule')->where(array('parent_id'=>$value['id']))->select();
        }
        $this->assign('rules',$rules);
        return $this->fetch();
    }
    /*
  * 客服组编辑
  * 赵坤
  * 20161206
  * */
    public function editGroup(){
        $this->assign('title','拼图云客服组编辑');
        $this->assign('description','拼图云商城');
        if($this->request->isPost()){
            $rules = $_POST['rule'];
            $rules = implode(',',$rules);
            $group_info = array(
                'title'=>$this->request->param('title'),
                'seller_id'=>session('shop'),
                'rules'=>$rules
            );
            $id = Db::name('shop_auth_group')->insertGetId($group_info);
            if($id!==false){
                return json(array('status'=>1));
            }else{
                return json(array('status'=>0));
            }
        }
        $rules = Db::name('shop_auth_rule')->where(array('parent_id'=>0))->select();
        foreach ($rules as $key=>$value){
            $rules[$key]['child']=Db::name('shop_auth_rule')->where(array('parent_id'=>$value['id']))->select();
        }
        $this->assign('rules',$rules);
        return $this->fetch();
    }
}